package top.heathylife.pagehelperx.factory;

import lombok.NonNull;
import top.heathylife.pagehelperx.other.BusinessException;
import top.heathylife.pagehelperx.converter.PredicateConverter;
import top.heathylife.pagehelperx.other.TypeFunction;
import top.heathylife.pagehelperx.comparator.AscComparator;
import top.heathylife.pagehelperx.comparator.DescComparator;
import top.heathylife.pagehelperx.converter.BetweenConverter;
import top.heathylife.pagehelperx.converter.EqConverter;
import top.heathylife.pagehelperx.converter.LikeConverter;
import top.heathylife.pagehelperx.enums.Operator;

import java.util.Comparator;

/**
 * 转换器,比较器 的 工厂类
 *
 * @author maoxiaomeng
 */
public class ConverterFactory {
    public static <T> PredicateConverter<T> getPredicateInstance(Operator operator) {
        if (operator == null) {
            throw new BusinessException("获取转换器失败,比较符为空");
        }
        switch (operator) {
            case EQ:
                return new EqConverter<T>();
            case LIKE:
                return new LikeConverter<T>();
            case BETWEEN:
                return new BetweenConverter<T>();
            default:
                throw new BusinessException("未实现的比较符:{}", operator.name());
        }
    }

    public static <T> Comparator<T> getOrderComparator(@NonNull Operator orderBy, TypeFunction<T, ?> key) {
        if (orderBy == null) {
            throw new BusinessException("获取比较器失败,参数为空");
        }

        switch (orderBy) {
            case ORDER_BY_ASC:
                return AscComparator.getInstance(key);
            case ORDER_BY_DESC:
                return DescComparator.getInstance(key);
            default:
                throw new BusinessException("参数错误,非预期的排序符:{}", orderBy.name());
        }
    }
}
